﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DonetArxLib
{
    public static class UsefulExtens
    {
        #region Equal

        public static bool AreValueEqual(this Point3d pt, Point3d other, double tol = 1e-3)
        {
            return Math.Abs(pt.X - other.X) <= tol
                && Math.Abs(pt.Y - other.Y) <= tol
                && Math.Abs(pt.Z - other.Z) <= tol;
        }

        public static bool AreValueEqual(this Vector2d vtx, Vector2d other, double tol = 1e-3)
        {
            return Math.Abs(vtx.X - other.X) < tol
                && Math.Abs(vtx.Y - other.Y) < tol;
        }

        public static bool AreValueEqual(this Vector3d vec, Vector3d other, double tol = 1e-6)
        {
            return Math.Abs(vec.X - other.X) < tol
                && Math.Abs(vec.Y - other.Y) < tol
                && Math.Abs(vec.Z - other.Z) < tol;
        }

        #endregion

        #region Polyline2d

        public static List<Vertex2d> GetVertexes(this Polyline2d pl)
        {
            var trans = UtilFunc.GetTopTransation();

            var result = new List<Vertex2d>();
            foreach (ObjectId vtxId in pl)
            {
                var vtx = trans.GetObject(vtxId, OpenMode.ForRead) as Vertex2d;
                if (vtx != null)
                    result.Add(vtx);
            }

            return result;
        }

        public static List<ObjectId> GetVertexIds(this Polyline2d pl)
        {
            var result = new List<ObjectId>();
            foreach (var id in pl)
                result.Add((ObjectId)id);

            return result;
        }

        #endregion

        #region Point2d, Point3d, Vector3d
        
        public static Point2d ToPoint2d(this Point3d pt)
        {
            return new Point2d(pt.X, pt.Y);
        }

        public static Point3d ToPoint3d(this Point2d pt, double z = 0)
        {
            return new Point3d(pt.X, pt.Y, z);
        }

        public static Vector3d ToVector3d(this Point3d pt)
        {
            return new Vector3d(pt.X, pt.Y, pt.Z);
        }

        public static Point3d ToPointd3d(this Vector3d vector)
        {
            return new Point3d(vector.X, vector.Y, vector.Z);
        }


        #endregion

#region RibbonUI

#if !_CAD2020
        public static RibbonTab AddTab(this RibbonControl ribbon, string title, string id, bool isActive)
        {
            RibbonTab tab = new RibbonTab();
            tab.Title = title;
            tab.Id = id;
            tab.IsActive = isActive;
            ribbon.Tabs.Add(tab);

            return tab;
        }

        public static RibbonPanelSource AddPanel(this RibbonTab tab, string title)
        {
            RibbonPanelSource ps = new RibbonPanelSource();
            ps.Title = title;
            RibbonPanel panel = new RibbonPanel();
            panel.Source = ps;

            tab.Panels.Add(panel);
            return ps;
        }
#endif

#endregion
    }
}
